﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bll;
using Model;
using System.Data;
using System.IO;

namespace Accounting.admin.Notice
{
    public partial class NoticeRevise : System.Web.UI.Page
    {
        BllPublic b = new BllPublic();
        ModelPublic m = new ModelPublic();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["id"] != null)
                {
                    m.Id = Request.QueryString["id"].ToString();
                    if (Session["Name"] != null && Session["Type"] != null)
                    {
                        UserType.Text = Session["Type"].ToString();
                        if (UserType.Text == "0")
                        {
                            Release.Visible = true;
                        }
                        else
                        {
                            Release.Visible = false;
                        }

                        ContentBind();
                    }
                    else
                    {
                        ASubmit.Disabled = false;
                        btnReturn.Enabled = false;
                    }
                }
                else
                {
                    Response.Redirect("./NoticeAdmin.aspx");
                }
            }
        }

        private void ContentBind()
        {
            DataTable dt = b.Notice_Week_Inquire_Single(m);

            foreach (DataRow my in dt.Rows)
            {
                txtTitle.Text = my["Title"].ToString();
                Content.Text = my["_Content"].ToString();

                string datetime = my["DateTime"].ToString();
                string date = "";

                for (int i = 0; i < 10; i++)
                {
                    date += datetime[i].ToString();
                }
                

                txtDateTime.Text = date;



                txtAuthor.Text = my["Author"].ToString();
                txtNumber.Text = my["Number"].ToString();
                lblFileName.Text = my["FileName"].ToString();
                lblFileRoute.Text = my["FileRoute"].ToString();
                if (my["Important"].ToString() == "True")
                    Important.Checked = true;
                else
                    Important.Checked = false;
                if (my["Release"].ToString() == "True")
                    Release.Checked = true;
                else
                    Release.Checked = false;
            }

        }

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["type"] != null)
            {
                if (Request.QueryString["type"].ToString() == "0")
                    Response.Redirect("./NoticeAdmin.aspx");
                else
                    Response.Redirect("./NoticeUnpublished.aspx");
            }

        }

        protected void btnRevise_Click(object sender, EventArgs e)
        {
            string folderRouteFile = "";

            m.Title ="'"+ txtTitle.Text.Trim() + "'";
            m.Content = Content.Text;
            m.DateTime= "'" + txtDateTime.Text.Trim() + "'";
            m.Author = "'" + txtAuthor.Text.Trim() + "'";
            m.Number = "'" + txtNumber.Text.Trim() + "'";

            if (Important.Checked)
                m.Important = "'True'";
            else
                m.Important = "'False'";

            if (UserType.Text == "0")
            {
                if (Release.Checked)
                    m.Release = "'True'";
                else
                    m.Release = "'False'";
            }


            if (ReleaseFile.HasFile)
            {
                string Upname = ReleaseFile.FileName;
                int Pointposition = 0;
                string FileName = "";
                string suffix = "";

                for (int i = 0; i < Upname.Length; i++)
                {
                    if (Upname[i] == '.')
                    {
                        Pointposition = i;
                        break;
                    }
                }

                for (int i = 0; i < Pointposition; i++)
                {
                    FileName += Upname[i];
                }

                for (int i = Pointposition; i < Upname.Length; i++)
                {
                    suffix += Upname[i];
                }

                m.DateTime = txtDateTime.Text.Trim();
                m.NavId = "12";
                int num = b.Notice_Week_Count_File_Number(m);

                string Time = System.DateTime.Now.ToString("yyyyMMdd");
                folderRouteFile = "InformationFile/" + Time +12+ (num + 1).ToString() + suffix;

                m.FileRoute = "'" + folderRouteFile + "'";
                m.FileName = "'" + ReleaseFile.FileName+"'";
            }
            else
            {
                if (lblFileName.Text == "")
                    m.FileName = "NULL";
                else
                    m.FileName = "'" + lblFileName.Text + "'";
                if (lblFileRoute.Text == "")
                    m.FileRoute = "NULL";
                else
                    m.FileRoute = "'" + lblFileRoute.Text + "'";
            }

            string message = b.Notice_Week_Modify(m);
            if (message == "1")
            {
                Response.Write("<script>alert('修改成功！')</script>");
                if(ReleaseFile.HasFile)
                {
                    try
                    {
                        ReleaseFile.SaveAs(Server.MapPath("../../" + folderRouteFile));
                    }
                    catch (Exception ex)
                    {
                        Response.Write("<script>alert('上传失败:" + ex.Message + "')</script> ");
                    }
                }
            }
            else
                Response.Write("<script>alert('" + message + "')</script>");

            ContentBind();

        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {

            DataTable dt = b.Notice_Week_Inquire_Single(m);

            foreach (DataRow my in dt.Rows)
            {

                if (my["FileName"].ToString() != "")
                {
                    string fileroute = my["FileRoute"].ToString();
                    int routeNum = 0;
                    for (int i = 0; i < fileroute.Length; i++)
                    {
                        if (fileroute[i].ToString() == ".")
                        {
                            routeNum = i;
                        }
                    }
                    string Suffix = "";
                    for (int i = routeNum; i < fileroute.Length; i++)
                    {
                        Suffix += fileroute[i];
                    }

                    string filename = my["FileName"].ToString() + Suffix;

                    string path = Server.MapPath("../../" + my["FileRoute"].ToString());
                    FileInfo fi = new FileInfo(path);
                    if (fi.Exists)
                    {
                        Response.Clear();
                        Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(filename));
                        Response.AddHeader("Content-Length", fi.Length.ToString());
                        Response.ContentType = "application/octet-stream;charset=gb2321";
                        Response.WriteFile(fi.FullName);
                        Response.Flush();
                        Response.Close();
                    }
                }
            }
        }
    }
}